package com.woniuxy.trade.service;

import com.woniuxy.common.dto.RoomUserDto;
import com.woniuxy.common.exceptions.Assert;
import com.woniuxy.common.exceptions.BusinessException;
import com.woniuxy.common.result.enums.impl.BusinessCode;
import com.woniuxy.common.utils.TokenUtil;
import com.woniuxy.common.vo.TokenVo;
import com.woniuxy.trade.entity.Room;
import com.woniuxy.trade.entity.RoomUser;
import com.woniuxy.trade.mapper.RoomMapper;
import com.woniuxy.trade.mapper.RoomUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class BuyerService {
    @Autowired
    RoomUserMapper roomUserMapper;
    @Autowired
    RoomMapper roomMapper;

    /**
     * 用户竞价
     * @param roomUserDto
     */
    public void offerBid(RoomUserDto roomUserDto) {
        //从token中拿到当前用户信息
        TokenVo tokenVo = TokenUtil.getTokenVo();
        Integer userid = tokenVo.getId();
        Integer roomid = roomUserDto.getRoomid();
        Integer productid = roomUserDto.getProductid();
        BigDecimal currprice = roomUserDto.getCurrprice();
        //查询中间表，判断是否缴纳保证金
        RoomUser roomUser = roomUserMapper.selectByUserIdAndRoomId(userid, roomid);
        Assert.error(roomUser, BusinessCode.NOT_Eligible_ERR);
        //查询房间表(redis)中的最高出价信息，判断是否可以更新数据库
        Room room = roomMapper.selectByPrimaryKey(roomid);
        BigDecimal currprice1 = room.getCurrprice();
        if(currprice1 != null){
            //比较价格的大小，判断是否可以替换
            if(currprice.compareTo(currprice1)==1){
                //使用乐观锁更新数据库
                roomMapper.updateRoomByCons(roomid,currprice,userid);
            }else {
                throw new BusinessException(BusinessCode.PRICE_ERR);
            }
        }else {
            roomMapper.updateRoomByCons(roomid,currprice,userid);
        }
        //发送websocket

    }

    /**
     * 发送websocket
     */
    public String websocket(){
        return null;
    }

    /**
     * 缴纳保证金
     * @param roomUserDto
     */
    public void payDeposit(RoomUserDto roomUserDto) {
        //从token中拿到当前用户信息
        TokenVo tokenVo = TokenUtil.getTokenVo();

        Integer userid = tokenVo.getId();
        Integer roomid = roomUserDto.getRoomid();
        Integer productid = roomUserDto.getProductid();
        Room room = roomMapper.selectByPrimaryKey(roomid);
        //封装中间表信息
        RoomUser roomUser = new RoomUser();
        roomUser.setRoomid(roomid);
        roomUser.setUserid(userid);
        roomUser.setPrice(room.getStartprice());
        roomUser.setStatus(0);
        roomUser.setProductid(productid);
        roomUser.setDepositrate(room.getDepositrate());
        roomUserMapper.insert(roomUser);

    }

    //查验用户是否缴纳保证金
    public Boolean havingDep(int roomid) {
        //从token中拿到当前用户信息
        TokenVo tokenVo = TokenUtil.getTokenVo();
        Integer userid = tokenVo.getId();
        RoomUser roomUser = roomUserMapper.selectByUserIdAndRoomId(userid, roomid);
        if (roomUser == null){
            return false;
        }
        return true;
    }

    /**
     * 查看房间是否可以竞价
     * @param roomid
     */
    public void isTime(int roomid) {
        Room room = roomMapper.selectByPrimaryKey(roomid);
        int status = room.getStatus();
        if(status != 1){
            throw new BusinessException(BusinessCode.ROOM_STATUS_ERR);
        }
    }
}
